package com.example.project.business;

import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.example.project.bootstrap.extend.EnableTransactionManagerImportAware;
import com.example.project.bootstrap.extend.ResourcelessTransactionManager;

//@EnableRetry
@EnableTransactionManagement
@EnableTransactionManagerImportAware
@Import(ResourcelessTransactionManager.class)
//不生效，AnnotationAsyncExecutionAspect 类没有在运行时拦截到 @Async方法，怀疑是没有ajc编译器
//@EnableAsync(mode = AdviceMode.ASPECTJ)
@EnableAsync
@Configuration
@ComponentScan
public class BusinessConfiguration {

	@Lazy
	@Bean(name = { "async-executor" })
	public ThreadPoolTaskExecutor initExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(20);
		executor.setMaxPoolSize(50);
		executor.setQueueCapacity(100);
		executor.setKeepAliveSeconds(60);
		executor.setThreadNamePrefix("async-executor-");
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.setWaitForTasksToCompleteOnShutdown(true);
		executor.initialize();
		return executor;
	}
}
